package main

import (
	"fmt"
	"gitee.com/dennis-kk/idl2lang/scli"
	"os"
)

const (
	toolVersion = "0.2.0"
)

func CheckIdlDir(idlname string) error {
	var err error
	if PathExits(idlname) == false {
		err = os.Mkdir(idlname, 0755)
		if err != nil {
			fmt.Printf("create srevice: %s  dir error: %v !!!\n", idlname, err)
			return err
		}
	}
	return nil
}

// CheckSrvSubDir 这里默认应该在impl 目录 获取最后一层目录做测试
func CheckSrvSubDir(path string) bool {
	var err error
	if PathExits(path) == false {
		err = os.Mkdir(path, 0755)
		if err != nil {
			fmt.Printf("make Service %s working path error %v !!!!", path, err)
			return false
		}
	}
	return true
}

func main() {
	flags := []*scli.FlagCfg{
		{
			BaseCfg: &scli.BaseCfg{
				Name:     "source",
				Short:    "s",
				Usage:    "读取frontend生成的json,解析代码生成信息",
				Required: true,
			},
			Dest: &opts.input,
		},
		{
			BaseCfg: &scli.BaseCfg{
				Name:     "out",
				Short:    "o",
				Usage:    "设置输出目录",
				Required: true,
			},
			Dest: &opts.outDir,
		},
		{
			BaseCfg: &scli.BaseCfg{
				Name:  "limit",
				Short: "l",
				Usage: "限制只生成某种服务",
			},
			Dest: &opts.limit,
		},
		{
			BaseCfg: &scli.BaseCfg{
				Name:  "proxy",
				Short: "p",
				Usage: "是否生成proxy模式",
			},
			Dest: &opts.proxy,
		},
		{
			BaseCfg: &scli.BaseCfg{
				Name:  "file",
				Short: "f",
				Usage: "输出文件名字",
			},
			Dest: &opts.toFile,
		},
		{
			BaseCfg: &scli.BaseCfg{
				Name:  "gensubdir",
				Usage: "是否生成子目录，仅code命令有效",
			},
			Default: "false",
			Dest:    &opts.genSubDir,
		},
		{
			BaseCfg: &scli.BaseCfg{
				Name:  "backend",
				Short: "b",
				Usage: "backend 版本",
			},
			Default: "latest",
			Dest:    &opts.backend,
		},
		{
			BaseCfg: &scli.BaseCfg{
				Name:  "runtime",
				Short: "r",
				Usage: "runtime 版本",
			},
			Default: "latest",
			Dest:    &opts.runtime,
		},
	}

	commands := []*scli.CommandCfg{
		{
			BaseCfg: &scli.BaseCfg{
				Name:  "code",
				Short: "c",
				Usage: "生成rpc框架代码相关代码，参数[template1,template2]",
			},
			Action: onCodeAction,
		},
		{
			BaseCfg: &scli.BaseCfg{
				Name:  "data",
				Short: "d",
				Usage: "生成rpc数据层struct代码，参数[template1,template2]",
			},
			Action: onDataAction,
		},
		{
			BaseCfg: &scli.BaseCfg{
				Name:  "table",
				Short: "t",
				Usage: "生成plato table代码，用于orm，参数[template1,template2]",
			},
			Action: onTableAction,
		},
		{
			BaseCfg: &scli.BaseCfg{
				Name:  "enum",
				Short: "e",
				Usage: "生成rpc数据层enum代码，参数[template1,template2]",
			},
			Action: onEnumAction,
		},
		{
			BaseCfg: &scli.BaseCfg{
				Name:  "sdk",
				Usage: "生成sdk版本代码模板 --proxy 模式不生成服务",
			},
			Action: onSDKAction,
		},
		{
			BaseCfg: &scli.BaseCfg{
				Name:  "pubgo",
				Usage: "生成pubgo 模板代码生成",
			},
			Action: onPubAction,
		},
	}

	var cliOpts []scli.Option
	for _, flag := range flags {
		cliOpts = append(cliOpts, scli.WithFlag(flag))
	}

	for _, command := range commands {
		cliOpts = append(cliOpts, scli.WithCommand(command))
	}

	cmd := scli.NewCmd(cliOpts...)
	err := cmd.Parse(os.Args)
	if err != nil {
		os.Exit(1)
	}
}
